package com.gxa.service.impl;

import com.gxa.annotation.LoginLog;
import com.gxa.dto.ResultDto;
import com.gxa.exception.SystemException;
import com.gxa.group.LoginName;
import com.gxa.mapper.AuthMapper;
import com.gxa.mapper.UserMapper;
import com.gxa.pojo.Auth;
import com.gxa.pojo.Menu;
import com.gxa.pojo.Role;
import com.gxa.pojo.User;
import com.gxa.service.LoginService;
import com.gxa.util.Response;
import com.gxa.validator.MyValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service("loginByName")
public class LoginServiceByNameImpl implements LoginService {
    @Autowired
    private MyValidator validator;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AuthMapper authMapper;

    @Override
    public ResultDto login(User user, HttpSession session) {
        // 校验
        validator.validate(user, LoginName.class);
        // 查询数据库
        User dbUser = userMapper.findByName(user);
        if (dbUser == null) {
            throw new SystemException(1003, "该用户不存在！");
        }
        // 判断密码
        if (!dbUser.getUserPwd().equals(user.getUserPwd())) {
            throw new SystemException(1004, "密码错误！");
        }
        // 登录成功
        session.setAttribute("userName", dbUser.getUserName());
        //判断用户是否为超级管理员
        Boolean isSuper = isSuper(dbUser);
        session.setAttribute("isSuper",isSuper);
        if(isSuper){
            //拿到所有菜单
            List<Auth> authList = authMapper.findAllAuth();
            List<Menu> menus = getMenu(authList);
            session.setAttribute("menus",menus);
        }else {
            List<Auth> authList = getAuth(dbUser);
            List<Menu> menus = getMenu(authList);
            session.setAttribute("menus",menus);
            session.setAttribute("authList",authList);
        }
        return Response.success("登录成功！");
    }

    /**
     * 判断用户是否为超级用户
     * @param user
     * @return
     */
    private Boolean isSuper(User user){
        List<Role> roles = user.getRoles();
        for (Role role : roles) {
            if(role.getIsSuper().equals(1)){
                return true;
            }
        }
        return false;
    }

    private List<Auth> getAuth(User user){
        List<Auth> authList = new ArrayList<>();
        List<Role> roles = user.getRoles();
        for (Role role : roles) {
            authList.addAll(role.getAuths());
        }
        return authList;
    }
    /**
     * 拿到菜单
     * @param authList
     * @return
     */
    private List<Menu> getMenu(List<Auth> authList) {
        List<Menu> menus = new ArrayList<>();
        for (Auth auth : authList) {
            if (auth.getAuthLevel().equals(1) && auth.getIsMenu().equals(1)) {
                menus.add(new Menu(auth.getAuthName(), auth.getAuthUrl(), auth.getAuthLevel(), auth.getAuthId(), null));
            }
        }
        for (Menu menu : menus) {
            List<Menu> subMenus = new ArrayList<>();
            for (Auth auth : authList) {
                if (auth.getIsMenu().equals(1) && auth.getAuthLevel().equals(2)) {
                    if (menu.getMenuId().equals(auth.getAuthParentId())) {
                        subMenus.add(new Menu(auth.getAuthName(), auth.getAuthUrl(), auth.getAuthLevel(), auth.getAuthId(), null));
                    }
                }
            }
            menu.setSubMenu(subMenus);
        }
        return menus;
    }
}
